package com.example.mq.mqserver.core;

import com.example.mq.common.ConsumerEnv;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 表示存储消息的队列
 */
public class MSGQueue {
    //队列的标识
    private String name;

    //表示是否要持久化
    private boolean durable = false;

    //表示队列是否私有化的？ false 表示非私有
    private boolean exclusive = false;

    private boolean autoDelete = false;

    private Map<String,Object> arguments = new HashMap<>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    public void setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
    }

    public boolean isAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

    //表示有哪些消费者进行了消息的订阅
    private List<ConsumerEnv> consumerEnvList = new ArrayList<>();

    //记录当前取到了第几个消费者 AtomicInteger 为原子性数据.避免并发产生的线程安全
    private AtomicInteger consumerSeq = new AtomicInteger(0);

    //添加一个新的订阅者
    public void addConsumerEnv(ConsumerEnv consumerEnv){
        consumerEnvList.add(consumerEnv);
    }

    //挑选一个订阅者处理当前的消息
    public ConsumerEnv chooseConsumer(){
        if(consumerEnvList.size() == 0){
            //该队列没有人可以订阅 --- 也就是没有消费者
            return null;
        }
        //计算当前要取的元素的下标
        int index =consumerSeq.get() % consumerEnvList.size();
        consumerSeq.getAndIncrement();
        return consumerEnvList.get(index);
    }

    public String getArguments(){
        //将 arguments 转换成 JSON 字符串存入数据库,解决数据库不能存储键值对的问题
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //出现异常了就返回空字符串
        return "{}";
    }

    public void setArguments(String argumentsJson) {
        //数据库读取数据 调用 setter方法
        //此时在setter方法内组装Map对象
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            this.arguments = objectMapper.readValue(argumentsJson, new TypeReference<HashMap<String,Object>>() {
            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    //Java 内部代码使用的 getter 和 setter.方便获取arguments的key value值
    public Object getArguments(String key){
        return arguments.get(key);
    }

    public void setArguments(String key,Object value){
        arguments.put(key,value);
    }

    public void setArguments(Map<String,Object> arguments){
        this.arguments = arguments;
    }
}
